package temaihui.daimaitemai;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.json.JSONObject;

import temaihui.daimaitemai.adapter.DiscoverCategoryGridAdapter;
import temaihui.daimaitemai.adapter.DiscoverTagGridAdapter;
import temaihui.daimaitemai.adapter.HomeCategoryGridAdapter;
import temaihui.daimaitemai.adapter.HomeGridAdapter;
import temaihui.daimaitemai.adapter.HomeRecommendGridAdapter;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link DiscoverFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link DiscoverFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DiscoverFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private DiscoverDataModel dataModel;

    private GridView tagGrid;
    private GridView categoryGrid;
    public DiscoverFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment DiscoverFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DiscoverFragment newInstance(String param1, String param2) {
        DiscoverFragment fragment = new DiscoverFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        tagGrid = (GridView)view.findViewById(R.id.discover_tag_grid);
        categoryGrid = (GridView)view.findViewById(R.id.discover_category_grid);

        getData();
        return view;
    }




    private void getData(){

        JSONObject data = new JSONObject();
        try {
            data.put("userId","");
        } catch (Exception e) {

        }
        RequestAction.sendRequest(getActivity(), "appserver/getDiscovery", data, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                renderViewByResponse(jsonObject);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });

    }
    private void renderViewByResponse(JSONObject jsonObject){
        try {
            dataModel = JSONHelper.parseObject(jsonObject, DiscoverResponseModel.class).getData();
            DiscoverTagGridAdapter tagGridAdapter = new DiscoverTagGridAdapter(getContext(),dataModel.getTagList());
            tagGrid.setAdapter(tagGridAdapter);
            tagGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getContext(), CategoryActivity.class);
                    intent.putExtra("categoryId", dataModel.getTagList().get(position).getId());
                    intent.putExtra("source", 1);
                    startActivity(intent);
                }
            });
            DiscoverCategoryGridAdapter categoryGridAdapter = new DiscoverCategoryGridAdapter(getContext(),dataModel.getCategoryList());
            categoryGrid.setAdapter(categoryGridAdapter);
            categoryGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getContext(), CategoryActivity.class);
                    intent.putExtra("categoryId", dataModel.getCategoryList().get(position).getId());
                    intent.putExtra("source", 2);
                    startActivity(intent);
                }
            });



        }catch (Exception e){

        }
    }
}
